package plus.easydo.server.user.mp.controller;

import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import plus.easydo.core.result.R;
import plus.easydo.server.user.mp.entity.SysDictData;
import plus.easydo.server.user.mp.service.ISysDictDataService;
import plus.easydo.server.user.mp.service.ISysDictTypeService;
import plus.easydo.starter.log.annotation.SystemLog;
import plus.easydo.starter.mybatis.plus.base.MPBaseController;
import plus.easydo.starter.oauth.core.utils.Oauth2Utils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 数据字典信息
 *
 * @author ruoyi
 */
@Api(tags = "数据字典信息")
@RestController
@RequestMapping("/dict/data")
public class SysDictDataController extends MPBaseController {

    @Resource
    private ISysDictDataService dictDataService;

    @Resource
    private ISysDictTypeService dictTypeService;

    @PreAuthorize("hasPermission('system:dict:list')")
    @GetMapping("/list")
    public R<List<SysDictData>> list(SysDictData dictData) {
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return ok(list);
    }

//    @PreAuthorize("hasPermission('system:dict:export')")
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, SysDictData dictData) throws IOException
//    {
//        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
//        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
//        util.exportExcel(response, list, "字典数据");
//    }

    /**
     * 查询字典数据详细
     */
    @PreAuthorize("hasPermission('system:dict:query')")
    @GetMapping(value = "/{dictCode}")
    public R<SysDictData> getInfo(@PathVariable Long dictCode) {
        return ok(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public R<List<SysDictData>> dictType(@PathVariable String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (Objects.isNull(data)) {
            data = new ArrayList<>();
        }
        return ok(data);
    }

    /**
     * 新增字典类型
     */
    @SystemLog(value = "新增字典类型",saveLog = true)
    @PreAuthorize("hasPermission('system:dict:add')")
    @PostMapping
    public R<Object> add(@Validated @RequestBody SysDictData dict) {
        dict.setCreateBy(Oauth2Utils.getUserName());
        return opResult(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    @SystemLog(value = "修改保存字典类型",saveLog = true)
    @PreAuthorize("hasPermission('system:dict:edit')")
    @PutMapping
    public R<Object> edit(@Validated @RequestBody SysDictData dict) {
        dict.setUpdateBy(Oauth2Utils.getUserName());
        return opResult(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
    @SystemLog(value = "删除字典类型",saveLog = true)
    @PreAuthorize("hasPermission('system:dict:remove')")
    @DeleteMapping("/{dictCodes}")
    public R<Object> remove(@PathVariable Long[] dictCodes) {
        return opResult(dictDataService.deleteDictDataByIds(dictCodes));
    }
}
